/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.datacenter.api.dto.*;
import org.cshoo.tattoo.datacenter.app.ConfigAppService;
import org.cshoo.tattoo.datacenter.domain.po.Config;
import org.cshoo.tattoo.datacenter.domain.po.ConfigItem;
import org.cshoo.tattoo.embed.infra.entity.PageQueryEntity;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Api(tags = "Config接口")
@RestController
@RequestMapping(value = "/config")
@Slf4j
public class ConfigController {

    @Resource
    private ConfigAppService configAppService;

    /**
     * 判断编号是否存在
     *
     * @param code 编号
     * @return 判断结果
     */
    @ApiOperation("校验config编号")
    @RequestMapping(value = "/checkConfigCodeExists", method = RequestMethod.GET)
    public ValidResp checkConfigCodeExists(@RequestParam String code) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = configAppService.checkConfigCodeExists(code);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加config
     *
     * @param addConfigReq 添加config request
     */
    @ApiOperation("添加config")
    @PostMapping(value = "/addConfig")
    public Response<?> addConfig(@RequestBody AddConfigReq addConfigReq) {
        log.info("addConfigReq:{}", addConfigReq);
        configAppService.addConfig(addConfigReq);
        return Response.builder().build();
    }

    /**
     * 修改config
     *
     * @param updateConfigReq 修改config request
     */
    @ApiOperation("修改config")
    @PutMapping(value = "/updateConfig")
    public Response<?> updateConfig(@RequestBody @Valid UpdateConfigReq updateConfigReq) {
        log.info("updateConfigReq:{}", updateConfigReq);
        configAppService.updateConfig(updateConfigReq);
        return Response.builder().build();
    }

    /**
     * 删除config
     *
     * @param id config id
     */
    @ApiOperation("删除config")
    @DeleteMapping(value = "/deleteConfig/{id}")
    public Response<?> deleteConfig(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        configAppService.deleteConfig(id);
        return Response.builder().build();
    }

    /**
     * 查询config
     *
     * @return config 列表
     */
    @ApiOperation("查询config列表")
    @GetMapping(value = "/queryConfigs")
    @Deprecated
    public List<Config> queryConfigs() {
        return configAppService.queryConfigs();
    }

    /**
     * 分页查询config
     *
     * @param request 请求VO
     * @return 分页查询config对象
     */
    @ApiOperation("查询config列表")
    @PostMapping(value = "/page/queryConfigs")
    public Page<Config> queryConfigs(@RequestBody @Valid PageQueryEntity<String> request) {
        return configAppService.queryConfigs(request);
    }

    /**
     * 判断编号是否存在
     *
     * @param code     config项编号
     * @param configId configId
     * @return 判断结果
     */
    @ApiOperation("校验config项编号")
    @RequestMapping(value = "/checkConfigItemCodeExists", method = RequestMethod.GET)
    public ValidResp checkConfigItemCodeExists(@RequestParam String code, @RequestParam Long configId) {
        log.info("code:{}", code);
        ValidResp result = new ValidResp();
        boolean exists = configAppService.checkConfigItemCodeExists(code, configId);
        result.setValid(!exists);
        return result;
    }

    /**
     * 判断environment是否存在
     *
     * @param configId    配置id
     * @param environment 环境
     * @return 判断结果
     */
    @ApiOperation("校验config编号")
    @RequestMapping(value = "/checkConfigItemEnvironmentExists", method = RequestMethod.GET)
    public ValidResp checkConfigItemEnvironmentExists(@RequestParam Long configId, @RequestParam String environment) {
        log.info("configId:{},environment:{}", configId, environment);
        ValidResp result = new ValidResp();
        boolean exists = configAppService.checkConfigItemEnvironmentExists(configId, environment);
        result.setValid(!exists);
        return result;
    }

    /**
     * 添加config 项
     *
     * @param addConfigItemReq 添加config项VO
     */
    @ApiOperation("添加config项")
    @PostMapping(value = "/addConfigItem")
    public Response<?> addConfigItem(@RequestBody AddConfigItemReq addConfigItemReq) throws Exception {
        log.info("addConfigItemReq:{}", addConfigItemReq);
        configAppService.addConfigItem(addConfigItemReq);
        return Response.builder().build();
    }

    /**
     * 修改config项
     *
     * @param updateConfigItemReq 修改config项VO
     */
    @ApiOperation("修改config项")
    @PutMapping(value = "/updateConfigItem")
    public Response<?> updateConfigItem(@RequestBody UpdateConfigItemReq updateConfigItemReq) {
        log.info("updateConfigItemReq:{}", updateConfigItemReq);
        configAppService.updateConfigItem(updateConfigItemReq);
        return Response.builder().build();
    }

    /**
     * 删除config项
     *
     * @param id config项id
     */
    @ApiOperation("删除config项")
    @DeleteMapping(value = "/deleteConfigItem/{id}")
    public Response<?> deleteConfigItem(@PathVariable("id") Long id) {
        log.info("id:{}", id);
        configAppService.deleteConfigItem(id);
        return Response.builder().build();
    }

    /**
     * 查询config项
     *
     * @param configId configId
     * @return config项
     */
    @ApiOperation("查询config项")
    @GetMapping(value = "/queryConfigItems/{configId}")
    public List<ConfigItem> queryConfigItems(@PathVariable("configId") Long configId) {
        log.info("configId:{}", configId);
        return configAppService.queryConfigItems(configId);
    }

    /**
     * 查询单个config
     *
     * @param code 编号
     * @return config响应对象
     */
    @ApiOperation("查询单个config，用于消费侧")
    @GetMapping(value = "/querySingleConfig/{code}")
    public QuerySingleConfigResp querySingleConfig(@PathVariable("code") String code) {
        log.info("code:{}", code);
        return configAppService.querySingleConfig(code);
    }
}
